package com.example.demo.controller;

import com.example.demo.constant.Code;
import com.example.demo.enumeration.ItemStatus;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.entity.Admin;
import com.example.demo.pojo.entity.Item;
import com.example.demo.pojo.entity.Report;
import com.example.demo.pojo.vo.CommentVO;
import com.example.demo.pojo.vo.ItemDetailVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.AdminService;
import com.example.demo.service.ItemService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private ItemService itemService;

    @PostMapping("/login")
    public Result login(@RequestBody @Valid Admin admin) {
        String username = admin.getUsername();

        Long id = adminService.selectIdByUsername(username);
        if (id == null) {
            return new Result(Code.LOGIN_ERR, null, "该管理员不存在");
        }

        String password = admin.getPassword();

        if (!adminService.checkPassword(username, password)) {
            return new Result(Code.LOGIN_ERR, null, "密码错误，请重试");
        }

        String token = adminService.generateAdminToken(id, username);

        return new Result(Code.LOGIN_OK, token, "登录成功");
    }

    @PostMapping("/exit")
    public Result exit() {
        Long currentAdminId = adminService.getCurrentAdminId();

        adminService.deleteToken(currentAdminId);

        return new Result(Code.EXIT_OK, null, "退出登录成功");
    }

    @GetMapping("/item/list")
    public Result getPendingItemList(@RequestParam(defaultValue = "1") int page) {
        PageVO pageVO = adminService.getPendingItemList(page);

        return new Result(Code.SELECT_OK, pageVO, "查询需要审核的商品列表成功");
    }

    @GetMapping("/item/{itemId}")
    public Result getItemDetail(@PathVariable Long itemId) {
        ItemDetailVO itemDetailVO = adminService.getItemDetail(itemId);

        int code = (itemDetailVO != null) ? Code.SELECT_OK : Code.SELECT_ERR;
        String message = (itemDetailVO != null) ? "查询商品详情成功" : "目标商品不存在";

        return new Result(code, itemDetailVO, message);
    }

    @PatchMapping("/item/review/{itemId}")
    public Result reviewItem(@PathVariable Long itemId) {
        ItemStatus status = itemService.selectStatusByItemId(itemId);

        if (status == null) {
            return new Result(Code.UPDATE_ERR, null, "该商品不存在");
        }

        if (!status.equals(ItemStatus.pending) && !status.equals(ItemStatus.rejected)) {
            return new Result(Code.UPDATE_ERR, null, "该商品已通过审核");
        }

        boolean result = adminService.updateItemStatus(itemId, ItemStatus.available);

        int code = (result) ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String message = (result) ? "通过商品审核成功" : "商品状态更新失败，请重试";

        return new Result(code, null, message);
    }

    @PatchMapping("/item/reject/{itemId}")
    public Result rejectItem(@PathVariable Long itemId) {
        ItemStatus status = itemService.selectStatusByItemId(itemId);

        if (status == null) {
            return new Result(Code.UPDATE_ERR, null, "该商品不存在");
        }

        if (!status.equals(ItemStatus.pending)) {
            return new Result(Code.UPDATE_ERR, null, "该商品状态不在审核中");
        }

        boolean result = adminService.updateItemStatus(itemId, ItemStatus.rejected);

        int code = (result) ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String message = (result) ? "拒绝商品审核成功" : "商品状态更新失败，请重试";

        return new Result(code, null, message);
    }

    @DeleteMapping("/item/{itemId}")
    public Result deleteItem(@PathVariable Long itemId) {
        ItemStatus status = itemService.selectStatusByItemId(itemId);
        if (status == null) {
            return new Result(Code.DELETE_ERR, null, "目标商品不存在");
        }

        if (!status.equals(ItemStatus.available)) {
            return new Result(Code.DELETE_ERR, null, "不能删除状态不为available的商品");
        }

        boolean result = itemService.deleteItem(itemId);

        int code = (result) ? Code.DELETE_OK : Code.DELETE_ERR;
        String message = (result) ? "删除商品成功" : "删除商品失败，请重试";

        return new Result(code, null, message);
    }

    @GetMapping("/item/comment/list")
    public Result getItemCommentList(@RequestBody @Validated(ValidateGroup.Crud.Read.class) Item item) {
        Long itemId = item.getId();

        ItemStatus status = itemService.selectStatusByItemId(itemId);
        if (status == null) {
            return new Result(Code.SELECT_ERR, null, "该商品不存在");
        }

        List<CommentVO> itemCommentList = itemService.getItemCommentList(itemId);

        return new Result(Code.SELECT_OK, itemCommentList, "查询商品评论成功");
    }

    @DeleteMapping("/item/comment/{commentId}")
    public Result deleteComment(@PathVariable Long commentId) {
        Long userId = itemService.selectUserIdByCommentId(commentId);

        if (userId == null) {
            return new Result(Code.DELETE_ERR, null, "目标评论不存在");
        }

        boolean result = itemService.deleteComment(commentId);

        int code = (result) ? Code.DELETE_OK : Code.DELETE_ERR;
        String message = (result) ? "删除评论成功" : "删除评论失败，请重试";

        return new Result(code, null, message);
    }

    @GetMapping("/item/report/list")
    public Result getItemReportList(@RequestParam(defaultValue = "1") int page) {
        PageVO pageVO = adminService.getItemReportList(page);

        return new Result(Code.SELECT_OK, pageVO, "查询商品举报信息列表成功");
    }

    @GetMapping("/item/comment/report/list")
    public Result getCommentReportList(@RequestParam(defaultValue = "1") int page) {
        PageVO pageVO = adminService.getCommentReportList(page);

        return new Result(Code.SELECT_OK, pageVO, "查询商品评论举报信息列表成功");
    }

    @DeleteMapping("/item/report/delete")
    public Result deleteItemReportByItemId(@RequestBody @Validated(ValidateGroup.ItemReport.class) Report report) {
        Long itemId = report.getItemId();

        ItemStatus status = itemService.selectStatusByItemId(itemId);
        if (status == null) {
            return new Result(Code.DELETE_ERR, null, "该商品不存在");
        }

        List<Long> reportIdList = adminService.selectReportIdListByItemId(itemId);
        if (reportIdList.size() == 0) {
            return new Result(Code.DELETE_OK, null, "删除举报信息成功");
        }

        boolean result = adminService.deleteReportByItemId(itemId);

        int code = (result) ? Code.DELETE_OK : Code.DELETE_ERR;
        String message = (result) ? "删除举报信息成功" : "删除举报信息失败，请重试";

        return new Result(code, null, message);
    }

    @DeleteMapping("/item/comment/report/delete")
    public Result deleteCommentReportByCommentId(@RequestBody @Validated(ValidateGroup.CommentReport.class) Report report) {
        Long commentId = report.getCommentId();

        Long userId = itemService.selectUserIdByCommentId(commentId);
        if (userId == null) {
            return new Result(Code.DELETE_ERR, null, "该评论不存在");
        }

        List<Long> reportIdList = adminService.selectReportIdListByCommentId(commentId);
        if (reportIdList.size() == 0) {
            return new Result(Code.DELETE_OK, null, "删除举报信息成功");
        }

        boolean result = adminService.deleteReportByCommentId(commentId);

        int code = (result) ? Code.DELETE_OK : Code.DELETE_ERR;
        String message = (result) ? "删除举报信息成功" : "删除举报信息失败，请重试";

        return new Result(code, null, message);
    }
}
